package com.example.airole.common.util;

import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

/**
 * 文件工具类
 */
public class FileUtils {
    
    /**
     * 支持的音频格式
     */
    public static final List<String> SUPPORTED_AUDIO_FORMATS = Arrays.asList(
        "wav", "mp3", "m4a", "aac", "flac", "ogg", "wma"
    );
    
    
    /**
     * 最大文件大小 (10MB)
     */
    public static final long MAX_FILE_SIZE = 10 * 1024 * 1024;
    
    /**
     * 最大音频文件大小 (50MB)
     */
    public static final long MAX_AUDIO_SIZE = 50 * 1024 * 1024;
    
    /**
     * 检查文件是否为支持的音频格式
     */
    public static boolean isSupportedAudioFormat(String filename) {
        String extension = FilenameUtils.getExtension(filename).toLowerCase();
        return SUPPORTED_AUDIO_FORMATS.contains(extension);
    }
    
    
    /**
     * 检查文件大小是否超限
     */
    public static boolean isFileSizeValid(long fileSize, long maxSize) {
        return fileSize <= maxSize;
    }
    
    /**
     * 检查音频文件大小是否有效
     */
    public static boolean isAudioFileSizeValid(long fileSize) {
        return isFileSizeValid(fileSize, MAX_AUDIO_SIZE);
    }
    
    /**
     * 检查普通文件大小是否有效
     */
    public static boolean isFileSizeValid(long fileSize) {
        return isFileSizeValid(fileSize, MAX_FILE_SIZE);
    }
    
    /**
     * 获取文件扩展名
     */
    public static String getFileExtension(String filename) {
        return FilenameUtils.getExtension(filename).toLowerCase();
    }
    
    /**
     * 获取不带扩展名的文件名
     */
    public static String getFileNameWithoutExtension(String filename) {
        return FilenameUtils.getBaseName(filename);
    }
    
    /**
     * 创建目录
     */
    public static void createDirectoryIfNotExists(String directoryPath) throws IOException {
        Path path = Paths.get(directoryPath);
        if (!Files.exists(path)) {
            Files.createDirectories(path);
        }
    }
    
    /**
     * 保存上传的文件
     */
    public static String saveUploadedFile(MultipartFile file, String targetDirectory) throws IOException {
        createDirectoryIfNotExists(targetDirectory);
        
        String originalFilename = file.getOriginalFilename();
        String extension = getFileExtension(originalFilename);
        String filename = System.currentTimeMillis() + "_" + originalFilename;
        
        Path targetPath = Paths.get(targetDirectory, filename);
        Files.copy(file.getInputStream(), targetPath);
        
        return targetPath.toString();
    }
    
    /**
     * 删除文件
     */
    public static boolean deleteFile(String filePath) {
        try {
            Path path = Paths.get(filePath);
            return Files.deleteIfExists(path);
        } catch (IOException e) {
            return false;
        }
    }
    
    /**
     * 检查文件是否存在
     */
    public static boolean fileExists(String filePath) {
        return Files.exists(Paths.get(filePath));
    }
    
    /**
     * 获取文件大小
     */
    public static long getFileSize(String filePath) {
        try {
            return Files.size(Paths.get(filePath));
        } catch (IOException e) {
            return 0;
        }
    }
}

